#include "config.h"

#include <glib.h>
#include <epan/packet.h>
#include <epan/value_string.h>

static int proto_udp_sample = -1;
static int hf_udp_version = -1;
static int hf_udp_id = -1;
static int hf_udp_buffer = -1;

static gint ett_udp = -1;

static void dissect_cpe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    gint offset = 0;
    guint16 data_len = -1;
    const gchar *cmd_str;
    
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "udp-sample");
    /* Clear out stuff in the info column */
    col_clear(pinfo->cinfo,COL_INFO);
    
    if (tree) { /* we are being asked for details */
        proto_item *ti = NULL;
        proto_tree *udp_tree = NULL;
        
        ti = proto_tree_add_item(tree, proto_udp_sample, tvb, 0, -1, FALSE);
        udp_tree = proto_item_add_subtree(ti, ett_udp);
        
        proto_tree_add_item(ti, hf_udp_version, tvb, offset, 2, FALSE);
        offset +=2;
        proto_tree_add_item(ti, hf_udp_id, tvb, offset, 4, FALSE);
        offset +=4;
       
        proto_tree_add_item(ti, hf_udp_buffer, tvb, offset, 1024, FALSE);
    }
}

/****************************************************************************
* FUNCTION NAME : proto_register_udp_sample
* DESCRIPTION   : 注册cpe协议到wireshark中
* INPUT         : 无
* OUTPUT        : 无
* RETURN        : 无
*****************************************************************************/
void proto_register_udp_sample(void)
{
    static hf_register_info hf[] = {
        { &hf_udp_version,
            { "Version", "Version",
            FT_UINT16, BASE_DEC,
            NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_udp_id,
            { "ID", "ID",
            FT_UINT32, BASE_DEC,
            NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_udp_buffer,
            { "Buffer", "Buffer",
            FT_STRING, BASE_NONE,
            NULL, 0x0,
            NULL, HFILL }
        },
    };

    /* Setup protocol subtree array */
    static gint *ett[] = {
        &ett_udp
    };
    
    
    proto_udp_sample = proto_register_protocol (
        "udp sample protocol",                       /* name       */
        "udp-sample",              /* short name */
        "udp-sample"               /* abbrev     */
        );
    
    proto_register_field_array(proto_udp_sample, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}


void proto_reg_handoff_udp_sample(void)
{
    dissector_handle_t udp_handle;

    udp_handle = create_dissector_handle(dissect_cpe, proto_udp_sample);
    dissector_add_uint("udp.port", 11110, udp_handle);
}


